﻿using System.Collections.Generic;
using System.IO;
using Windows.Graphics.Imaging;

namespace DNFImagePacks2Manager.NPK
{
    public class IMGV2File : IMGFile
    {
        private static readonly List<int> bitmapType = [0x10, 0x0F, 0x0E];

        public IMGV2File(BinaryReader reader, IMGHeader imgHeader) : base(imgHeader)
        {
            for (int i = 0; i < imgHeader.Count; i++)
            {
                Images.Add(ImageFile.ReadImageFile(reader));
            }
            foreach (var image in Images)
            {
                int size = image.GetSize();
                if (size > 0)
                {
                    image.DataBytes = reader.ReadBytes(size);
                }
            }
        }

        public IMGV2File(IMGHeader imgHeader, List<ImageFile> list) : base(imgHeader)
        {
            imgHeader.Version = 2;
            Images.AddRange(list);
        }

        public override bool SetImage(int index, ImageFile image)
        {
            if (base.SetImage(index, image))
            {
                return true;
            }
            else
            {
                if (index < Images.Count)
                {
                    if (image is ImageBitmapFile bitmapImage)
                    {
                        if (bitmapType.Contains(bitmapImage.Type.TypeCode))
                        {
                            Images[index] = bitmapImage;
                            return true;
                        }
                    }
                    return false;
                }
                else
                {
                    return false;
                }
            }
        }

        public override SoftwareBitmap? GetPreviewImage(int index, bool ignorePointer = true)
        {
            SoftwareBitmap? bitmap = base.GetPreviewImage(index, ignorePointer);
            if (bitmap == null)
            {
                if (index < Images.Count) {
                    ImageFile image = Images[index];
                    if (image is ImagePointerFile pointerImage && !ignorePointer)
                    {
                        return GetPreviewImage(pointerImage.Pointer, ignorePointer);
                    }
                    if (image is ImageBitmapFile bitmapImage) {
                        return bitmapImage.GetPreviewImage();
                    }
                }
            }
            return bitmap;
        }

        public override byte[] GetBytes()
        {
            List<byte> bytes = [];
            List<byte> indexBytes = [];
            List<byte> dataBytes = [];
            foreach (var image in Images)
            {
                indexBytes.AddRange(image.GetIndexBytes());
                if (image.GetSize() > 0)
                {
                    dataBytes.AddRange(image.DataBytes);
                }
            }
            bytes.AddRange(GetHeaderBytes(indexBytes.Count));
            bytes.AddRange(indexBytes);
            bytes.AddRange(dataBytes);
            return [.. bytes];
        }
    }
}
